{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 198,
   "id": "f045099e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "from mlxtend.frequent_patterns import apriori # 生产频繁项集\n",
    "from mlxtend.frequent_patterns import association_rules # 生产关联规则\n",
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 199,
   "id": "66a65c57",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>Goods</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>, Milk</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>, Onions</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>,Yoghurt</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>,Spices</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>,Kidney Beans</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>2</td>\n",
       "      <td>,EggS</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>7</td>\n",
       "      <td>,Yoghurt</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>2</td>\n",
       "      <td>,Yoghurt</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>2</td>\n",
       "      <td>,Yoghurt</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   id          Goods\n",
       "0   1        , Milk \n",
       "1   1       , Onions\n",
       "2   1       ,Yoghurt\n",
       "3   1        ,Spices\n",
       "4   1  ,Kidney Beans\n",
       "5   2          ,EggS\n",
       "6   7       ,Yoghurt\n",
       "7   2      ,Yoghurt \n",
       "8   2       ,Yoghurt"
      ]
     },
     "execution_count": 199,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "order_data = pd.read_csv('./data/GoodsOrder_eng.csv',header=0,encoding='gbk')\n",
    "# 转换数据格式\n",
    "order_data['Goods']= order_data['Goods'].apply(lambda x: ',' + x)\n",
    "order_data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3b791097",
   "metadata": {},
   "source": [
    "1.将 order_data按id 分组求和，并重置索引。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 200,
   "id": "67d91a07",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>index</th>\n",
       "      <th>id</th>\n",
       "      <th>Goods</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>, Milk , Onions,Yoghurt,Spices,Kidney Beans</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>,EggS,Yoghurt ,Yoghurt</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>7</td>\n",
       "      <td>,Yoghurt</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   index  id                                        Goods\n",
       "0      0   1  , Milk , Onions,Yoghurt,Spices,Kidney Beans\n",
       "1      1   2                       ,EggS,Yoghurt ,Yoghurt\n",
       "2      2   7                                     ,Yoghurt"
      ]
     },
     "execution_count": 200,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#由考生填写\n",
    "order_data = order_data.groupby('id',as_index=False)['Goods'].sum().reset_index()\n",
    "#由考生填写\n",
    "order_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 201,
   "id": "545232e3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>index</th>\n",
       "      <th>id</th>\n",
       "      <th>Goods</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>[ Milk , Onions,Yoghurt,Spices,Kidney Beans]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>[EggS,Yoghurt ,Yoghurt]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>7</td>\n",
       "      <td>[Yoghurt]</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   index  id                                         Goods\n",
       "0      0   1  [ Milk , Onions,Yoghurt,Spices,Kidney Beans]\n",
       "1      1   2                       [EggS,Yoghurt ,Yoghurt]\n",
       "2      2   7                                     [Yoghurt]"
      ]
     },
     "execution_count": 201,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "order_data['Goods'] = order_data['Goods'].apply(lambda x: [x[1:]]) # 加了[]\n",
    "order_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 202,
   "id": "02a6848a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[' Milk , Onions,Yoghurt,Spices,Kidney Beans'],\n",
       " ['EggS,Yoghurt ,Yoghurt'],\n",
       " ['Yoghurt']]"
      ]
     },
     "execution_count": 202,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "order_data_list = list(order_data['Goods'])\n",
    "order_data_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "69c99348",
   "metadata": {},
   "source": [
    "2.分割商品名为每一个元素。使得 dataset 最终输出为以下格式:"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "46f9f2fd",
   "metadata": {},
   "source": [
    "列表是最常用的Python数据类型，它可以作为一个方括号内的逗号分隔值出现。\n",
    "列表的数据项不需要具有相同的类型\n",
    "创建一个列表，只要把逗号分隔的不同的数据项使用方括号括起来即可"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 203,
   "id": "19d3e4d1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[' Milk ', ' Onions', 'Yoghurt', 'Spices', 'Kidney Beans'],\n",
       " ['EggS', 'Yoghurt ', 'Yoghurt'],\n",
       " ['Yoghurt']]"
      ]
     },
     "execution_count": 203,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_transaction = []\n",
    "for i in order_data_list:\n",
    "    #由考生填写  # i->['Milk,Onions,Yoghurt,Spices,KidneyBeans'] i[0]->'Milk,Onions,Yoghurt,Spices,KidneyBeans'\n",
    "    p = i[0].split(',')\n",
    "    #由考生填写\n",
    "    # print(i[0])\n",
    "    data_transaction.append(p)\n",
    "data_transaction "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 204,
   "id": "94fba5e3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[[' Milk ', ' Onions', 'Yoghurt', 'Spices', 'Kidney Beans'],\n",
       " ['EggS', 'Yoghurt ', 'Yoghurt'],\n",
       " ['Yoghurt']]"
      ]
     },
     "execution_count": 204,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataSet = data_transaction\n",
    "column_list = []\n",
    "for var in dataSet:\n",
    "    column_list = set(column_list)|set(var)\n",
    "dataSet"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3d1dacfe",
   "metadata": {},
   "source": [
    "3.遍历 dataSet 中的每一个商品,并将 data 中对应位置的值加1,即购买一次则在相应物品上加 1，使得 data 输出为以下形式:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 205,
   "id": "cf5c2225",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{' Milk ', ' Onions', 'EggS', 'Kidney Beans', 'Spices', 'Yoghurt', 'Yoghurt '}"
      ]
     },
     "execution_count": 205,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "column_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 207,
   "id": "59a65c41",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "data = pd.DataFrame(np.zeros((len(dataSet),7)),columns=column_list)\n",
    "#data = pd.DataFrame(np.zeros(len(dataSet),7),columns=column_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 211,
   "id": "95c0e4ed",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Yoghurt</th>\n",
       "      <th>EggS</th>\n",
       "      <th>Onions</th>\n",
       "      <th>Milk</th>\n",
       "      <th>Kidney Beans</th>\n",
       "      <th>Spices</th>\n",
       "      <th>Yoghurt</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Yoghurt  EggS   Onions   Milk   Kidney Beans  Spices  Yoghurt \n",
       "0        1     0        1       1             1       1         0\n",
       "1        1     1        0       0             0       0         1\n",
       "2        1     0        0       0             0       0         0"
      ]
     },
     "execution_count": 211,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "for i in range(len(dataSet)):\n",
    "    for j in dataSet[i]:\n",
    "        #由考生填写\n",
    "        data[j][i] += 1\n",
    "        #由考生填写\n",
    "data= data.applymap(lambda x:1 if x>0 else 0)\n",
    "data  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9169acff",
   "metadata": {},
   "source": [
    "4.使用 Apriori 算法从数据中计算频繁项集，并将最小支持度设置为0.02。然后根据支持度倒排序，最后返回频繁项集 frequent_itemsets，使得frequent itemsets 部分输出为:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 185,
   "id": "230763a9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>support</th>\n",
       "      <th>itemsets</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>(Yoghurt)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>0.333333</td>\n",
       "      <td>(Kidney Beans, Spices)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>0.333333</td>\n",
       "      <td>(Yoghurt, Kidney Beans, Onions)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>0.333333</td>\n",
       "      <td>(Yoghurt, Spices, Onions)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>0.333333</td>\n",
       "      <td>(Yoghurt, EggS, Yoghurt )</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>0.333333</td>\n",
       "      <td>(Yoghurt, Milk , Kidney Beans)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>0.333333</td>\n",
       "      <td>(Yoghurt, Milk , Spices)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>0.333333</td>\n",
       "      <td>(Yoghurt, Kidney Beans, Spices)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>0.333333</td>\n",
       "      <td>(Milk , Kidney Beans, Onions)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>0.333333</td>\n",
       "      <td>(Milk , Spices, Onions)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>0.333333</td>\n",
       "      <td>(Spices, Kidney Beans, Onions)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>0.333333</td>\n",
       "      <td>(Milk , Kidney Beans, Spices)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31</th>\n",
       "      <td>0.333333</td>\n",
       "      <td>(Yoghurt, Milk , Kidney Beans, Onions)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>0.333333</td>\n",
       "      <td>(Yoghurt, Milk , Spices, Onions)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>33</th>\n",
       "      <td>0.333333</td>\n",
       "      <td>(Yoghurt, Spices, Kidney Beans, Onions)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>34</th>\n",
       "      <td>0.333333</td>\n",
       "      <td>(Yoghurt, Milk , Kidney Beans, Spices)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>35</th>\n",
       "      <td>0.333333</td>\n",
       "      <td>(Milk , Spices, Kidney Beans, Onions)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>0.333333</td>\n",
       "      <td>(Yoghurt, Milk , Onions)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>0.333333</td>\n",
       "      <td>(Milk , Spices)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.333333</td>\n",
       "      <td>(Onions)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>0.333333</td>\n",
       "      <td>(Milk , Kidney Beans)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.333333</td>\n",
       "      <td>(EggS)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.333333</td>\n",
       "      <td>(Milk )</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.333333</td>\n",
       "      <td>(Kidney Beans)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0.333333</td>\n",
       "      <td>(Spices)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>0.333333</td>\n",
       "      <td>(Yoghurt )</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>0.333333</td>\n",
       "      <td>(Yoghurt, Onions)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>0.333333</td>\n",
       "      <td>(Yoghurt, EggS)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>0.333333</td>\n",
       "      <td>(Yoghurt, Milk )</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>0.333333</td>\n",
       "      <td>(Yoghurt, Kidney Beans)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>0.333333</td>\n",
       "      <td>(Yoghurt, Spices)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>0.333333</td>\n",
       "      <td>(Yoghurt, Yoghurt )</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>0.333333</td>\n",
       "      <td>(Milk , Onions)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>0.333333</td>\n",
       "      <td>(Kidney Beans, Onions)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>0.333333</td>\n",
       "      <td>(Spices, Onions)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>0.333333</td>\n",
       "      <td>(EggS, Yoghurt )</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>36</th>\n",
       "      <td>0.333333</td>\n",
       "      <td>(Yoghurt, Spices, Onions, Milk , Kidney Beans)</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     support                                        itemsets\n",
       "0   1.000000                                       (Yoghurt)\n",
       "19  0.333333                          (Kidney Beans, Spices)\n",
       "21  0.333333                 (Yoghurt, Kidney Beans, Onions)\n",
       "22  0.333333                       (Yoghurt, Spices, Onions)\n",
       "23  0.333333                       (Yoghurt, EggS, Yoghurt )\n",
       "24  0.333333                  (Yoghurt, Milk , Kidney Beans)\n",
       "25  0.333333                        (Yoghurt, Milk , Spices)\n",
       "26  0.333333                 (Yoghurt, Kidney Beans, Spices)\n",
       "27  0.333333                   (Milk , Kidney Beans, Onions)\n",
       "28  0.333333                         (Milk , Spices, Onions)\n",
       "29  0.333333                  (Spices, Kidney Beans, Onions)\n",
       "30  0.333333                   (Milk , Kidney Beans, Spices)\n",
       "31  0.333333          (Yoghurt, Milk , Kidney Beans, Onions)\n",
       "32  0.333333                (Yoghurt, Milk , Spices, Onions)\n",
       "33  0.333333         (Yoghurt, Spices, Kidney Beans, Onions)\n",
       "34  0.333333          (Yoghurt, Milk , Kidney Beans, Spices)\n",
       "35  0.333333           (Milk , Spices, Kidney Beans, Onions)\n",
       "20  0.333333                        (Yoghurt, Milk , Onions)\n",
       "18  0.333333                                 (Milk , Spices)\n",
       "1   0.333333                                        (Onions)\n",
       "17  0.333333                           (Milk , Kidney Beans)\n",
       "2   0.333333                                          (EggS)\n",
       "3   0.333333                                         (Milk )\n",
       "4   0.333333                                  (Kidney Beans)\n",
       "5   0.333333                                        (Spices)\n",
       "6   0.333333                                      (Yoghurt )\n",
       "7   0.333333                               (Yoghurt, Onions)\n",
       "8   0.333333                                 (Yoghurt, EggS)\n",
       "9   0.333333                                (Yoghurt, Milk )\n",
       "10  0.333333                         (Yoghurt, Kidney Beans)\n",
       "11  0.333333                               (Yoghurt, Spices)\n",
       "12  0.333333                             (Yoghurt, Yoghurt )\n",
       "13  0.333333                                 (Milk , Onions)\n",
       "14  0.333333                          (Kidney Beans, Onions)\n",
       "15  0.333333                                (Spices, Onions)\n",
       "16  0.333333                                (EggS, Yoghurt )\n",
       "36  0.333333  (Yoghurt, Spices, Onions, Milk , Kidney Beans)"
      ]
     },
     "execution_count": 185,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#由考生填写\n",
    "frequent_itemsets=apriori(data,min_support=0.02,use_colnames=True)\n",
    "frequent_itemsets.sort_values(by='support',ascending=False,inplace=True)\n",
    "#由考生填写\n",
    "frequent_itemsets"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "12e53015",
   "metadata": {},
   "source": [
    "5.使用 assoeiation_rules 从频繁项集 frequentitemsets中构建关联规则，metric 为'confidence’min_threshold=0.35.对关联规则按 confidence值进行降序排列,并将排序结果存储在association rule中，使得 dfassociation_rule 部分输出为:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 172,
   "id": "a87589b3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>antecedents</th>\n",
       "      <th>consequents</th>\n",
       "      <th>antecedent support</th>\n",
       "      <th>consequent support</th>\n",
       "      <th>support</th>\n",
       "      <th>confidence</th>\n",
       "      <th>lift</th>\n",
       "      <th>leverage</th>\n",
       "      <th>conviction</th>\n",
       "      <th>zhangs_metric</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: [antecedents, consequents, antecedent support, consequent support, support, confidence, lift, leverage, conviction, zhangs_metric]\n",
       "Index: []"
      ]
     },
     "execution_count": 172,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#由考生填写\n",
    "association_rule =association_rules(df=frequent_itemsets,metric='confidence',min_threshold=0.35,support_only=True)\n",
    "association_rule.sort_values(by='confidence',ascending=False,inplace=True)\n",
    "#由考生填写\n",
    "df_association_rule=pd.DataFrame(association_rule)\n",
    "df_association_rule"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "15c70674",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
